
<template>
  <div>
    <!-- 特殊用户保修期维护 -->
    <!-- 搜索 -->
    <TopFilter ref="topFilters" :list="topFilterInit" :cols="3" labelWidth="90" @filterChange="changeHandle"></TopFilter>
    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :params="fetchParams"
      :fetchapi="fetchapi"
      :datakey="'records'"
      :isColumnFilter = "false"
      :onColumnsChange="(columns) => (this.columns = columns)"
      :onRowSelectChange="selectChange"
      :isSelectColumn="true"
      height="auto"
    >
    </FilterTable>
    <div class="dialogBottmView">
      <el-button @click="saveDialog()"  type="primary">保 存</el-button>
      <el-button @click="closeDialog()">关 闭</el-button>
    </div>
  </div>
</template>
<script>
import { findAllVIN,getAllVIN} from '@/api/claim/factory/SpecialUserWarrantyMaintain';
import { dictionary } from '@/utils/dictMixin';
export default {
  mixins: [dictionary],
  components: {},
  props: {
    rowData: {
      type: Object,
      required: true
    },
    ttRpVhlPeriodSpVinDTOList: {
      type: Array,
      required: false
    }
  },
  data() {
    return {
      fetchapi: findAllVIN,
      topFilterInit: this.topFilterInitFun(),
      flterValue: {}, //参数
      columns: this.creatHeader(),
      fetchParams: {}, //ipa 接口
      selectedRows: [], //选中经销商
      type:'',
      spId:'',
      visibleAdd: false,
      selectData:[],
      isRepeat:0,
      isAllVIN:[],
    };
  },
  async created() {
   let res = await getAllVIN();
   console.log("res=",res)
   this.isAllVIN = res.data;
   console.log("isAllVIN=",this.isAllVIN)
  },
  methods: {
    selectChange(val) {
      console.log("val1111111================",val)
      if(val.length>0){
        this.isRepeat=0;
        this.isAllVIN.forEach(item =>{
          val.forEach(x =>{
            console.log('vin',item,x);
            if(item === x.vin&&this.isRepeat==0){
              this.isRepeat ++;
              return this.$notify.error({title: '提示信息', message: x.vin+'已添加，请确认'});
            }
          })
        })
        this.ttRpVhlPeriodSpVinDTOList.forEach(item =>{
          val.forEach(x =>{
            console.log('vin',item,x);
            if(item.vin === x.vin&&this.isRepeat==0){
              this.isRepeat ++;
              return this.$notify.error({title: '提示信息', message: x.vin+'已添加，请确认'});
            }
          })
        })
        this.selectData = val;
      } else {
        this.selectData = [];
      }
    },
    async saveDialog() {
      this.loading = true;
      // this.isRepeat = false;
      if(this.selectData.length === 0){
        return this.$notify.error({title: '提示信息', message: '请选择保存VIN'});
      }
      console.log(this.selectData)


      if(this.selectData.length>0&&this.isRepeat==0){
        this.$emit('save',this.selectData);
      }

      // let res = await newSaveVIN({data:this.selectData,spId:this.rowData.spId});
      // console.log(res);
      // if (res.resultCode === 200) {
      //   this.$emit('save');
      //   return this.$notify.success({title: '提示信息', message: '保存成功'});
      // } else {
      //   this.loading = false;
      // }
    },
    //取消操作
    closeDialog(type) {
      if(type == 'refresh'){
        this.$emit('save');
      } else {
        this.$emit('close', { visibleAdd: true, data: this.selectedRows, type: 'close' });
      }

    },
    async saveAdd() {

      this.visibleDrawer = false;
      this.fetchParams = { ...this.fetchParams };
    },

    //搜索获取数据
    changeHandle(val) {
      this.fetchParams = Object.assign({}, this.fetchParams, val);
      console.log(val);
    },
    //搜索数据
    topFilterInitFun() {
      return [
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请输入VIN...',
          initialValue: '',
        },
      ];
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width:70,
          fixed: 'left',
          sorter: true,
          render: (props) => {
            return <span>{props.row.index + 1}</span>;
          },
        },
        { title: 'VIN', dataIndex: 'vin', sorter: true },
        { title: '车型', dataIndex: 'modelName', sorter: true },
        { title: '交车日期',dataIndex: 'inCarDate', sorter: true },
      ];
    }
  }
};
</script>

<style lang="less" scoped></style>
